import 'package:flustars/flustars.dart';
import 'package:fzcgx_app/network/http.dart';
import 'package:fzcgx_app/ui/base/base_view_model.dart';
import 'package:fzcgx_app/ui/login/page/login_page.dart';
import 'package:fzcgx_app/ui/order/model/order_release_model.dart';
import 'package:nav_router/nav_router.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class OrderReleaseViewModel extends BaseViewModel {
  RefreshController refreshController =
      RefreshController(initialRefresh: false);
  List<OrderReleaseModel> models;
  int page = 0;
  OrderReleaseViewModel() {
    this.setState(ViewState.Loading);
  }
  void requestApi() {
    Http.instance.postList<OrderReleaseModel>(
        'order/pub-index', {'page': this.page.toString(), 'type': '1'},
        onSuccess: (data) {
      refreshController.refreshCompleted();
      refreshController.loadComplete();
      if (data != null) {
        if (this.page == 0) {
          models = data;
        } else {
          models.addAll(data);
        }

        this.setState(ViewState.Success);
      } else {
        models = List<OrderReleaseModel>();
        this.setState(ViewState.Empty);
      }
    }, onError: (error) {
      this.setState(ViewState.Error);
    }, onFail: (fail) {
      if (fail.code == -1) {
        routePush(LoginPage()).then((value) {
          if (SpUtil.getString('uid').isNotEmpty) {
            this.requestApi();
          }
        });
      }
      this.setState(ViewState.Error);
    });
  }

  void onRefresh() async {
    this.page = 0;
    this.requestApi();
  }

  void onLoading() async {
    this.page++;
    this.requestApi();
  }
}
